﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Xml.Linq;

namespace Bing
{
    /// <summary>
    /// Contains an array of results returned for a query to the InstantAnswer <see cref="ApiHelper.SourceType"/>.
    /// </summary>
    [DebuggerDisplay("Total={Total}, Results={Results.Count}, Errors={Errors.Count}")]
    public class InstantAnswerResponse: SearchResponseBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="InstantAnswerResponse"/> class.
        /// </summary>
        public InstantAnswerResponse(XContainer element)
            : base(element)
        {
            Results = new List<InstantAnswerResult>();
        }

        /// <summary>
        /// Gets or sets an array of InstantAnswer results returned for this request.
        /// </summary>
        /// <value>Array of InstantAnswer results returned for this request.</value>
        public List<InstantAnswerResult> Results { get; set; }
        
        /// <summary>
        /// Parses the response from the Bing API.
        /// </summary>
        /// <param name="element">The XML from the Bing API</param>
        /// <returns>An <see cref="InstantAnswerResponse" /></returns>
        public static InstantAnswerResponse ParseElement(XContainer element)
        {
            InstantAnswerResponse instantAnswerResponse = new InstantAnswerResponse(element);
            if (instantAnswerResponse.Errors.Count > 0) return instantAnswerResponse;

            var instantAnswer = element.Element(XName.Get("InstantAnswer", ApiHelper.InstantAnswerNs));
            if (instantAnswer == null) return instantAnswerResponse;

            XElement instantAnswerResults = instantAnswer.Element(XName.Get("Results", ApiHelper.InstantAnswerNs));

            if (instantAnswerResults == null) return instantAnswerResponse;
            foreach (XElement result in instantAnswerResults.Elements(XName.Get("InstantAnswerResult", ApiHelper.InstantAnswerNs)))
            {
                InstantAnswerResult instantAnswerResult = new InstantAnswerResult();
                instantAnswerResult.Title = ApiHelper.GetElementValueOrDefault<string>(result, "Title", ApiHelper.InstantAnswerNs);
                instantAnswerResult.Url = ApiHelper.GetElementValueOrDefault<string>(result, "Url", ApiHelper.InstantAnswerNs);
                instantAnswerResult.ContentType = ApiHelper.GetElementValueOrDefault<string>(result, "ContentType", ApiHelper.InstantAnswerNs);
                instantAnswerResult.ClickThroughUrl = ApiHelper.GetElementValueOrDefault<string>(result, "ClickThroughUrl", ApiHelper.InstantAnswerNs);
                instantAnswerResult.Attribution = ApiHelper.GetElementValueOrDefault<string>(result, "Attribution", ApiHelper.InstantAnswerNs);
                XElement instantAnswerSpecificData =
                    result.Element(XName.Get("InstantAnswerSpecificData", ApiHelper.InstantAnswerNs));

                if (instantAnswerSpecificData != null)
                {
                    if (instantAnswerResult.ContentType == "FlightStatus")
                    {
                        XElement flightStatusElement =
                            instantAnswerSpecificData.Element(XName.Get("FlightStatus", ApiHelper.FlightStatusNs));
                        instantAnswerResult.FlightStatus = FlightStatus.ParseFlightStatus(flightStatusElement);
                    }
                    else
                    {
                        XElement encartaElement =
                            instantAnswerSpecificData.Element(XName.Get("Encarta", ApiHelper.EncartaNs));
                        instantAnswerResult.Encarta = Encarta.ParseEncarta(encartaElement);
                    }
                }

                instantAnswerResponse.Results.Add(instantAnswerResult);
            }

            return instantAnswerResponse;
        }
    }
}
